package yesjava;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Main2 {
    public static void main(String[] args) throws Exception {
        List<User> userList = new ArrayList<>();
//        获取连接
        Connection connection = MyDbUtil.getConnection();
//        3. 通过数据库连接（connection）获取  statement 对象
        String sql = " select * from user where id between ? and  ? ";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,5);
        statement.setInt(2,20);
        ResultSet resultSet = null;
//        4. 执行SQL 语句
        if(statement.execute()){
            resultSet = statement.getResultSet();
//            5. 处理结果
            while (resultSet.next()){
                Integer id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String password = resultSet.getString("password");
                Date createTime = resultSet.getTimestamp("create_time");
                userList.add(new User(id,name,password,createTime));
            }
        }
//        6. 关闭资源
        resultSet.close();
        statement.close();
        MyDbUtil.closeConnection();

        userList.forEach(System.out::println);

    }
}
